package day17;

import java.util.Scanner;

public class Demo implements Runnable{
    //Scanner sc = new Scanner(System.in);
    int sum=100;
    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        for (int i = 0; i < 100; i++) {
            synchronized (Demo.class) {
                if(sum>0){
                    sum--;
                    System.out.println(name+"购买成功"+sum);
                    //让其他线程买  自己不能买
                    //wait只能通过锁对象调用wait否则会出错
                    //原因在于  释放对象锁  需要这个对象  要不不知道释放那把锁
                    try {
                        Demo.class.notify();
                        Demo.class.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }
    public static void main(String[] args) {
        Demo demo = new Demo();
        new Thread(demo,"asd").start();
        new Thread(demo,"abc").start();
    }
}
